#!/usr/bin/python3
import pymumble_py3
import time
import subprocess
import argparse
import re
import os.path
from classfile import *
from tkinter import *
'''
parser = argparse.ArgumentParser(description='Interactive command line bot for mumble')
parser.add_argument('--url', metavar='mumble://example:42069', required=True, help='A url to the mumble server')
parser.add_argument('--name', metavar="BotName", dest='name', required=True, help='name of the bot')
args = parser.parse_args()

s1 = args.url

# parse url
host = re.search('mumble://.{3,250}\.[a-z0-9]{2,5}(:|/|\?)',s1).group(0)[:len(host)-1].replace('mumble://','')
port = 64738
if re.search(':[0-9]{4,5}', s1):
    port = re.search('[0-9]{4,5}', s1).group(0)
print("DEBUG: " + str(host))
print("DEBUG: " + str(port))

x = IffyAh(host, port, "cert.pem", "key.pem", args.name)
#x.move_into_every_channel()
def report_server(x):
    x.print_user_names()
    x.count_channels()

report_server(x)
while True:
    tmp = subprocess.call('clear',shell=True) 
    print()
    print("------------------------------------")
    print("0: Move into channel with most users")
    print("1: Move into every channel")
    print("2: Move into random channel")
    print("3: Print channel names")
    print("4: Print user names")
    print("5: Play file")
    print("6: Play file stick on users")
    print("7: Send global message")
    print("8: Robot voice")
    print("9: Play classy")
    print("99: Quit")
    print("------------------------------------")
    try:
        i = int(input("(select options 0-99)-> "))
    except:
        i = 90000 
    if i == 99:
        del x
        break
      
    if i == 0: x.move_into_most_users_channel()
    if i == 1: x.move_into_every_channel(); print("starting long running command...")
    if i == 2: x.move_into_random_channel()  
    if i == 3: x.print_channel_names(); input("press enter to continue")
    if i == 4: x.print_user_names()   
    if i == 5: x.play_file("./audio/grilly.wav") 
    if i == 6: x.play_file_stick_on_users("./audio/grilly.wav")
    if i == 9: x.play_file_stick_on_users("./audio/classy-ad.wav")
    if i == 7: x.send_global_message(input("ENTER MESSAGE > "))
    if i == 8: voice_text = input("ENTER VOICE MESSAGE > ")
    pass
'''

HOST=""
PORT=""
def clicked():
    s1 = "Welcome to " + txt1.get()

    host = re.search('mumble://.{3,250}\.[a-z0-9]{2,5}(:|/|\?)',s1).group(0)
    host = host[:len(host)-1].replace('mumble://','')
    port = 64738
    if re.search(':[0-9]{4,5}', s1):
        port = re.search('[0-9]{4,5}', s1).group(0)
    lbl1.configure(text=host)
    lbl2.configure(text=str(port))
    HOST=host
    PORT=port

def connectMumble():
    x = IffyAh(lbl1.cget("text"), lbl2.cget("text"), "cert.pem", "key.pem", "DEFAULT")
    def report_server(x):
        x.print_user_names()
        x.count_channels()
    report_server(x)
    x.m.dc()
window = Tk()
window.title("Mumble Tester")
window.geometry('850x600')
lbl1 = Label(window, text="", font=("Arial Bold", 15)) 
lbl2 = Label(window, text="", font=("Arial Bold", 15)) 
txt1 = Entry(window,width=70, font=("Arial Bold", 15))
txt2 = Entry(window,width=10, font=("Arial Bold", 15))
btn = Button(window, text="Click Me", command=clicked, font=("Arial Bold", 15))
connect = Button(window, text="Connect", command=connectMumble, font=("Arial Bold", 15))

btn.grid(column=0, row=0)
lbl1.grid(column=0, row=1)
lbl2.grid(column=0, row=2)
txt1.grid(column=0, row=3)
connect.grid(column=0, row=4)

window.mainloop()